package com.yunxi.service.localService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.DateUtil;
import com.yunxi.dao.otd.OtdInterfaceConfigMapper;
import com.yunxi.model.otd.OtdInterfaceConfig;
import com.yunxi.service.localService.otd.OtdInterfaceConfigService;
import com.yunxi.service.localService.otd.OtdProduceCalendarService;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * SO、CIR接口时间设置 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-10
 */
@Service
public class OtdInterfaceConfigServiceImpl extends
    ServiceImplPlus<OtdInterfaceConfigMapper, OtdInterfaceConfig> implements OtdInterfaceConfigService {


    @Autowired
    @Qualifier("otdProduceCalendarServiceImpl")
    OtdProduceCalendarService otdProduceCalendarService;

    /**
     * 根据年月、接口类型查询已发送的日期
     *
     * @param yearMonth 年月 201907
     * @param type 接口类型 SO/PSO/CIR
     * @return List<Integer>
     */
    @Override
    public List<Integer> findAlreadySendDayList(String yearMonth, String type) {
        List<Integer> dayList = new ArrayList<>();
        // 月第一天
        Date startDate = DateUtil.parse(yearMonth + "01", "yyyyMMdd");
        if (startDate == null) {
            return null;
        }
        // 月最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int monthEndDay = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(Calendar.DAY_OF_MONTH, monthEndDay);
        Date endDate = calendar.getTime();

        // 根据年月、接口类型查询已发送的记录
        List<OtdInterfaceConfig> list = baseMapper.findAlreadySendByYearMonth(startDate, endDate, type);
        list.forEach(config -> {
            //接口设置需求开始日、结束日
            int startDay = Integer.parseInt(DateUtil.format(config.getStartDate(), "dd"));
            int endDay = Integer.parseInt(DateUtil.format(config.getEndDate(), "dd"));
            // 开始时间在月第一天之前，则从1号开始
            if (config.getStartDate().before(startDate)) {
                startDay = 1;
            }
            // 结束时间在月最后一天之后，则到月底最后一天为止
            if (config.getEndDate().after(endDate)) {
                endDay = monthEndDay;
            }
            for (int i = startDay; i <= endDay; i++) {
                dayList.add(i);
            }
        });

        // 排除非工作日
        // 根据年月查询非工作日
        List<Date> noWorkList = otdProduceCalendarService.findWorkDayByYearMonth(yearMonth, "0");
        if(!CollectionUtils.isEmpty(noWorkList)){
            for (Date date : noWorkList) {
                dayList.remove(Integer.valueOf(DateUtil.format(date,"dd")));
            }
        }
        return dayList;
    }

    /**
     * 根据ID删除未同步下发设置
     *
     * @param ids ID
     */
    @Override
    public void removeByIds(List<String> ids) {
        LambdaQueryWrapper<OtdInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OtdInterfaceConfig::getId,ids).eq(OtdInterfaceConfig::getState,"W");
        this.remove(wrapper);
    }
}
